package com.errands.cn.controller;

import com.errands.cn.entity.Orders;
import com.errands.cn.entity.User;
import com.errands.cn.services.OrderService;
import com.errands.cn.utils.Json;
import com.errands.cn.utils.StringUtil;
import org.hibernate.validator.constraints.EAN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    @PostMapping("/add")
    public Json addOrder(Orders order, HttpServletRequest request){
        Json json = new Json();
       /* User user = (User) request.getSession().getAttribute("user");
        if (user == null){
            json.setMsg("你还没登录，无法发布订单");
            return json;
        }*/
        System.out.println(order.getSendAddress());
        order.setoId(StringUtil.getUUID());
        order.setoState(0);
        order.setoDate(new Date());
        order.setUid(12);
        if (orderService.saveOrder(order)){
            json.setMsg("发布订单成功，等待有人接单");
            json.setSuccess(true);
        }else {
            json.setMsg("发布订单失败，请检查订单信息是否填写完整");
        }
        return json;
    }

    @GetMapping("/cancel")
    public Json cancelOrder(@RequestParam("oId") String oId){
        Json json = new Json();
        Orders order1 = orderService.findByOrderId(oId);
        if (order1.getoState() != 0){
            json.setMsg("该订单已经被接单，无法取消");
            return json;
        }
        Orders order = new Orders();
        order.setoId(oId);
        order.setoState(4);
        if(orderService.updateOrder(order)){
            json.setSuccess(true);
        }else {
            json.setMsg("取消订单是失败");
        }
        return json;
    }

    @PostMapping("/update")
    public boolean updateOrder(Orders order){
        return orderService.updateOrder(order);
    }

    @GetMapping("/get_orders")
    public List<Orders> getOrdersByentity(Orders orders,HttpServletRequest request){
        try {
            User user = (User) request.getSession().getAttribute("user");
            if (user != null){
                orders.setUid(user.getUid());
            }
            if (!orders.getTitle().isEmpty()){
                orders.setTitle("%"+orders.getTitle()+"%");
            }
        }catch (NullPointerException e){
        }
        return orderService.getOrdersByentity(orders);
    }

    @GetMapping("/refresh")
    public boolean refreshOrder(@RequestParam("oId") String oid){
        Orders orders = new Orders();
        orders.setoId(oid);
        orders.setoDate(new Date());
        return orderService.updateOrder(orders);
    };

    @GetMapping("/confrim_recived")
    public boolean confirmRecived(@RequestParam("oId") String oid){
        Orders orders = new Orders();
        orders.setoId(oid);
        orders.setoState(2);
        return orderService.updateOrder(orders);
    };

    @GetMapping("/delete")
    public boolean delete(@RequestParam("oId") String oid){
        return orderService.deleteOrder(oid);
    }

    @GetMapping("/get_order")
    public Orders getOrderById(@RequestParam("oId") String oid){
        return orderService.findByOrderId(oid);
    }

    @PostMapping("/get_orders/select")
    public List<Orders> getOrdersByPrice(@RequestParam("minPrice") Integer minPrice,@RequestParam("maxPrice") Integer maxPrice,
                                         @RequestParam("title") String title,@RequestParam("goodsType") Integer goodsType,@RequestParam("oType") Integer oType){
      if (title.isEmpty()){
          title = null;
       }else {
          title = "%"+title+"%";
      }
        return orderService.getOrdersByPrice(minPrice,maxPrice,title,goodsType,oType);
    }



}
